package com.ruoyi.goods_manage.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.goods_manage.mapper.CategoryMapper;
import com.ruoyi.goods_manage.domain.Category;
import com.ruoyi.goods_manage.service.ICategoryService;
import org.springframework.util.ObjectUtils;

/**
 * 分类Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-28
 */
@Service
public class CategoryServiceImpl implements ICategoryService
{
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询分类
     *
     * @param id 分类主键
     * @return 分类
     */
    @Override
    public Category selectCategoryById(Long id)
    {
        return categoryMapper.selectCategoryById(id);
    }

    /**
     * 查询分类列表
     *
     * @param category 分类
     * @return 分类
     */
    @Override
    public List<Category> selectCategoryList(Category category)
    {
        return categoryMapper.selectCategoryList(category);
    }


    /**
     * 新增分类
     *
     * @param category 分类
     * @return 结果
     */
    @Override
    public int insertCategory(Category category)
    {
        return categoryMapper.insertCategory(category);
    }

    /**
     * 修改分类
     *
     * @param category 分类
     * @return 结果
     */
    @Override
    public int updateCategory(Category category)
    {
        return categoryMapper.updateCategory(category);
    }

    /**
     * 批量删除分类
     *
     * @param ids 需要删除的分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByIds(Long[] ids)
    {
        return categoryMapper.deleteCategoryByIds(ids);
    }

    /**
     * 删除分类信息
     *
     * @param id 分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryById(Long id)
    {
        return categoryMapper.deleteCategoryById(id);
    }

    @Override
    public List findAllCategory(Category category) {
        List<Category> categories = categoryMapper.selectCategoryList(category);
        List<Map<String, Object>> mapList = categories.stream()
                .map(c -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("label", c.getName());
                    map.put("value", c.getId());
                    return map;
                })
                .collect(Collectors.toList());
        return mapList;
    }

    @Override
    public Map statisticsCategory() {
        List<Map<String, Object>> sta = categoryMapper.statisticsCategory();
        //获取所有的名称 封装成list
        List<String> nameList = sta.stream().filter(map-> !ObjectUtils.isEmpty(map.get("name"))).map(v -> v.get("name").toString()).collect(Collectors.toList());
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("data",sta);
        hashMap.put("categoryData",nameList);
        return hashMap;
    }


}
